<template>
	<view class="wrap">
			<u-toast ref="uToast" />
		<u-form :model="model" :rules="rules" ref="uForm">
			
		<u-form-item  label-width="150" label="原密码" prop="originalPassword">
			<u-input :password-icon="true" :border="border" type="number" v-model="model.originalPassword" placeholder="默认密码123456"></u-input>
		</u-form-item>
			<u-form-item  label="密码" prop="password">
				<u-input :password-icon="true" type="password" v-model="model.password" placeholder="请输入密码"></u-input>
			</u-form-item>
			<u-form-item  label="确认密码" label-width="150" prop="confirmPassword">
				<u-input  type="password" v-model="model.confirmPassword" placeholder="请确认密码"></u-input>
			</u-form-item>
		</u-form>
		<u-button @click="submit">提交</u-button>
	</view>
</template>

<script>
	
	export default{
		data(){
			return {
				model: {
					originalPassword:'',
					password: '',
					confirmPassword: '',
				},
				rules: {
					
					originalPassword: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === uni.getStorageSync('password')
							},
							message: '原密码错误',
							trigger: ['blur'],
						}
					],
					password: [
						{
							required: true,
							message: '请输入密码',
							trigger: ['change', 'blur'],
						},
						{
							// 正则不能含有两边的引号
							pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]+\S{5,12}$/,
							message: '需同时含有字母和数字，长度在6-12之间',
							trigger: ['change', 'blur'],
						}
					],
					confirmPassword: [
						{
							required: true,
							message: '请重新输入密码',
							trigger: ['change', 'blur'],
						},
						{
							validator: (rule, value, callback) => {
								return value === this.model.password;
							},
							message: '两次输入的密码不相等',
							trigger: ['change', 'blur'],
						}
					],
				},
				
				
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		methods:{
			submit() {
					this.$refs.uForm.validate(valid => {//修改密码的验证
						if (valid) {
							this.$api.newPass(this.model).then(
							res=>{
									this.$refs.uToast.show({
										duration: 3000,
										title: '修改成功',
										type: 'success',
										url:'/pages/Login/Login'
									});				
							}
							)
						} else {
							console.log('验证失败');
						}
					})
				},
		}
	}

</script>

<style scoped lang="scss">
	.wrap {
		padding: 30rpx;
	}

	.agreement {
		display: flex;
		align-items: center;
		margin: 40rpx 0;

		.agreement-text {
			padding-left: 8rpx;
			color: $u-tips-color;
		}
	}
</style>
